package io.geekidea.springbootplus.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.geekidea.springbootplus.config.properties.SpringBootPlusProperties;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.core.validator.groups.Add;
import io.geekidea.springbootplus.framework.core.validator.groups.Update;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.geekidea.springbootplus.framework.util.UploadUtil;
import io.geekidea.springbootplus.system.entity.SysProduct;
import io.geekidea.springbootplus.system.param.SysProductPageParam;
import io.geekidea.springbootplus.system.service.SysProductService;
import io.geekidea.springbootplus.system.vo.SysProductAttributeQueryVo;
import io.geekidea.springbootplus.system.vo.SysProductQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sysProduct")
@Module("system")
@Api(value = "商品信息API", tags = {"商品信息"})
public class SysProductController extends BaseController {

    @Resource
    private SysProductService sysProductService;


    @PostMapping("/add")
    @OperationLog(name = "添加商品信息", type = OperationLogType.ADD)
    @ApiOperation(value = "添加商品信息", response = ApiResult.class)
    public ApiResult<Boolean> addSysUser(@Validated(Add.class) @RequestBody SysProduct sysProduct) throws Exception {

        boolean flag = sysProductService.saveSysProduct(sysProduct);
        return ApiResult.result(flag);
    }


    /**
     * 修改商品信息
     */
    @PostMapping("/update")
    @OperationLog(name = "修改商品信息", type = OperationLogType.UPDATE)
    @ApiOperation(value = "修改商品信息", response = ApiResult.class)
    public ApiResult<Boolean> updateSysProduct(@Validated(Update.class) @RequestBody SysProduct sysProduct) throws Exception {
        boolean flag = sysProductService.updateSysProduct(sysProduct);
        return ApiResult.result(flag);
    }


    @GetMapping("/info/{id}")
    @OperationLog(name = "商品信息详情", type = OperationLogType.INFO)
    @ApiOperation(value = "商品信息详情", notes = "")
    public ApiResult<SysProductQueryVo> getSysUser(@PathVariable("id") Integer id) throws Exception {
        SysProductQueryVo sysProductQueryVo = sysProductService.findSysProduct(id);
        return ApiResult.ok(sysProductQueryVo);
    }


    @PostMapping("/delete/{id}")
    @OperationLog(name = "删除商品信息", type = OperationLogType.DELETE)
    @ApiOperation(value = "删除商品信息", response = ApiResult.class)
    public ApiResult<Boolean> deleteSysProduct(@PathVariable("id") Integer id) throws Exception {
        boolean flag = sysProductService.deleteSysProduct(id);
        return ApiResult.result(flag);
    }

    @PostMapping("/getPageList")
    @OperationLog(name = "商品信息分页列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "商品信息分页列表", response = SysProductQueryVo.class)
    public ApiResult<Paging<SysProductQueryVo>> getSysProductPageListByParam( @RequestBody SysProductPageParam sysProductPageParam) throws Exception {
        Paging<SysProductQueryVo> paging = sysProductService.getSysProductPageListByParam(sysProductPageParam);
        return ApiResult.ok(paging);
    }


    @PostMapping("/batch/up")
    //@RequiresPermissions("sys:cart:deleteAll")
    @OperationLog(name = "批量商品上架")
    @ApiOperation(value = "批量商品上架", response = ApiResult.class)
    public ApiResult<Boolean> batchSysProductUp(@RequestBody List<Integer> ids) throws Exception {
        boolean flag = sysProductService.updateBatchSysProductUpStatus(ids);
        return ApiResult.result(flag);
    }

    @PostMapping("/batch/down")
    //@RequiresPermissions("sys:cart:deleteAll")
    @OperationLog(name = "批量商品下架")
    @ApiOperation(value = "批量商品下架", response = ApiResult.class)
    public ApiResult<Boolean> batchSysProductDown(@RequestBody List<Integer> ids) throws Exception {
        boolean flag = sysProductService.updateBatchSysProductDownStatus(ids);
        return ApiResult.result(flag);
    }


    @PostMapping("/delete")
    //@RequiresPermissions("sys:cart:deleteAll")
    @OperationLog(name = "批量逻辑删除")
    @ApiOperation(value = "批量逻辑删除", response = ApiResult.class)
    public ApiResult<Boolean> batchDeleteSysProduct(@RequestBody List<Integer> ids) throws Exception {
        boolean flag = sysProductService.deleteBatchSysProduct(ids);
        return ApiResult.result(flag);

    }


    @PostMapping("/sales")
    @OperationLog(name = "查询热销前4名", type = OperationLogType.PAGE)
    @ApiOperation(value = "查询热销前4名", response = SysProductQueryVo.class)
    public ApiResult<List<SysProductQueryVo>> getSysProductBySales() throws Exception {
        List<SysProductQueryVo> sysProductBySales = sysProductService.findSysProductBySales();
        return ApiResult.ok(sysProductBySales);
    }


    @PostMapping("/tags")
    @OperationLog(name = "查询任意标签前4名", type = OperationLogType.PAGE)
    @ApiOperation(value = "查询任意标签前4名", response = SysProductQueryVo.class)
    public ApiResult<List<SysProductQueryVo>> getSysProductByTags(@RequestParam String tags) throws Exception {
        List<SysProductQueryVo> sysProductByTags = sysProductService.getSysProductByTags(tags);
        return ApiResult.ok(sysProductByTags);
    }





}
